package product;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JLabel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

public class Stock extends JInternalFrame {
	private JTextField text1;
	private JTextField text2;
	private JTextField text3;
	private JTextField text4;
	private JButton btnNewButton;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JLabel lblNewLabel_3;

	HashMap<String, StockData> sto = new HashMap<String, StockData>();

	/**
	 * Create the frame.
	 */
	public Stock(HashMap<String, StockData> sto) {
		super("sto", true, true, true, false);
		this.sto = sto;
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(null);
		getContentPane().add(getText1());
		getContentPane().add(getText2());
		getContentPane().add(getText3());
		getContentPane().add(getText4());
		getContentPane().add(getBtnNewButton());
		getContentPane().add(getLblNewLabel());
		getContentPane().add(getLblNewLabel_1());
		getContentPane().add(getLblNewLabel_2());
		getContentPane().add(getLblNewLabel_3());

	}

	private JTextField getText1() {
		if (text1 == null) {
			text1 = new JTextField();
			text1.setBounds(50, 22, 132, 21);
			text1.setColumns(10);
		}
		return text1;
	}

	private JTextField getText2() {
		if (text2 == null) {
			text2 = new JTextField();
			text2.setColumns(10);
			text2.setBounds(50, 53, 132, 21);
		}
		return text2;
	}

	private JTextField getText3() {
		if (text3 == null) {
			text3 = new JTextField();
			text3.setColumns(10);
			text3.setBounds(50, 84, 132, 21);
		}
		return text3;
	}

	private JTextField getText4() {
		if (text4 == null) {
			text4 = new JTextField();
			text4.setColumns(10);
			text4.setBounds(50, 115, 132, 21);
		}
		return text4;
	}

	private JButton getBtnNewButton() {
		if (btnNewButton == null) {
			btnNewButton = new JButton("저장");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String code = text1.getText();
					String name = text2.getText();
					int price = Integer.parseInt(text3.getText());// (,)와(원)을 제거
					int ea = Integer.parseInt(text4.getText());

					StockData data = new StockData(code, name, price, ea);
					sto.put(code, data);
					try {// 수정이되면서 파일을 저장
						FileOutputStream fw = new FileOutputStream(
								"StockList.dat");
						ObjectOutputStream bw = new ObjectOutputStream(fw);
						bw.writeObject(sto);// 저장
						bw.close();
						fw.close();
						System.out.println("저장 성공");
					} catch (Exception e) {
						System.out.println("error");
					}
				}
			});
			btnNewButton.setBounds(50, 146, 97, 23);
		}
		return btnNewButton;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("제품코드");
			lblNewLabel.setBounds(207, 25, 57, 15);
		}
		return lblNewLabel;
	}

	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("제품명");
			lblNewLabel_1.setBounds(207, 56, 57, 15);
		}
		return lblNewLabel_1;
	}

	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("단가");
			lblNewLabel_2.setBounds(207, 87, 57, 15);
		}
		return lblNewLabel_2;
	}

	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("재고");
			lblNewLabel_3.setBounds(207, 118, 57, 15);
		}
		return lblNewLabel_3;
	}
}
